# This code is part of the MWP System
# Copyright (c) 2012 Benjamin Lubin (blubin@bu.com) 
# Published under and subject to the GPLv2 license available at http://www.gnu.org/licenses/gpl-2.0.html

'''
Created on Dec 13, 2012
@author: blubin
'''

from backend.backend import Backend;
from workflow.task import Task;
from workflow.result import Result;
from workflow.flowData import Status;
#from unique import UNIQUE;

class CoffeeBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "CoffeeImport", dashboard=True); #need unique?

    def wire(self):
        #farmer tasks
        self.register_result_listener("farmer", "alertf", self.alertf_complete)
        self.register_result_listener("farmer", "sendSample", self.sendSample_complete)
        self.register_result_listener("farmer", "sendFull", self.sendFull_complete)
        #roaster tasks
        self.register_result_listener("roaster", "request", self.request_complete)
        self.register_result_listener("roaster", "accept", self.accept_complete)
        self.register_result_listener("roaster", "roast", self.roast_complete)
        self.register_result_listener("roaster", "package", self.package_complete)
        self.register_result_listener("roaster", "alertr", self.alertr_complete) 
        #distributor tasks
        self.register_result_listener("distr", "buy", self.buy_complete)
        self.register_result_listener("distr", "ship", self.ship_complete)

        #self.register_joined_listener([("distr", "FillPrescription", Result), ("Physician", "IsPrescriptionNeeded", Result, Status.COMPLETE)], self.fill_prescription_predicate, self.fill_prescription_complete)

    def alertf_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "roaster", "request");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def request_complete(self, results):
#        for result in results:
#            task = Task.construct_from_result(result, "farmer", "sendSample");
#            self.workflow.add(task);
#            self.workflow.update_status(result, Status.COMPLETE);

        for result in results:
            if result.data['request'] == "TRUE":
                #Add task to write the needed prescrption
                task = Task.construct_from_result(result, "farmer", "sendSample");
                self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);
#                
#                #Add task to ask if we need another prescription
#                task = Task.construct_from_result(result, "farmer", "requestcomplete");
#                task.set_field('prescriptionnumber',result.get_int_field('prescriptionnumber')+1);
#                task.remove_field('isprescriptionneeded');
#                self.workflow.add(task);
#            self.workflow.update_status(result, Status.COMPLETE);


    def sendSample_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "roaster", "accept");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def accept_complete(self, results):
        for result in results:
            if result.data['accept'] == "TRUE":   
                task = Task.construct_from_result(result, "farmer", "sendFull");
                self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);
        


    def sendFull_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "roaster", "roast");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);
        
#        
#        for result in results:
#            task = Task.construct_from_result(result, "Physician", "IsPrescriptionNeeded");
#            task.set_field('prescriptionnumber', 1);
#            self.workflow.add(task);
#            copyFields =['FirstName','LastName','Birthday','InsuranceCompany','CoPay'];
#            task = Task.construct_from_result(result, "Billing", "BillInsurance", copy=copyFields);
#            self.workflow.add(task);
#            self.workflow.update_status(result, Status.COMPLETE);

    def roast_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "roaster", "package");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);        
         
        
#        # One of the results should have isprescriptionneeded=FALSE.  That is the total number.  Ensure we have that number.
#        lastpreneeded = [result for result in results if (result.stepname == "IsPrescriptionNeeded" and not result.get_bool_field('isprescriptionneeded'))];
#        if len(lastpreneeded) == 0:
#            return False;
#        # One less than the "No" is the number we are looking for:
#        numprescriptions = lastpreneeded[0].get_int_field("prescriptionnumber")-1;
#        # Get the filled ones:
#        filled = [result for result in results if result.stepname == "FillPrescription"];
#        # If they are all filled, we are done:
#        return len(filled) == numprescriptions;



    def package_complete(self, results):
        
        for result in results:
            task = Task.construct_from_result(result, "roaster", "alertr");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);             
        
        
        
#        # Get the filled ones:
#        filled = [result for result in results if result.stepname == "FillPrescription"];
#        
#        if len(filled) > 0:
#            copyFields =['FirstName','LastName','Birthday'];
#            appendFields = ['prescriptionnumber', 'DrugName','GenericName','Refills'];
#            addFields = ['TotalCharge'];
#            task = Task.construct_from_results(filled, "Pharmacist", "DispensePrescription", copy=copyFields, append=appendFields, add_fields=addFields);
#            self.workflow.add(task);
#        for result in filled:
#            self.workflow.update_status(result, Status.COMPLETE);
            
            
    def alertr_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "distr", "buy");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);        
       
        
#        # Get the filled ones:
#        filled = [result for result in results if result.stepname == "FillPrescription"];
#        
#        if len(filled) > 0:
#            copyFields =['FirstName','LastName','Birthday'];
#            appendFields = ['prescriptionnumber', 'DrugName','GenericName','Refills'];
#            addFields = ['TotalCharge'];
#            task = Task.construct_from_results(filled, "Pharmacist", "DispensePrescription", copy=copyFields, append=appendFields, add_fields=addFields);
#            self.workflow.add(task);
#        for result in filled:
#            self.workflow.update_status(result, Status.COMPLETE);
            
    def buy_complete(self, results):
        for result in results:
            if result.data['buy'] == "TRUE":  
                task = Task.construct_from_result(result, "distr", "ship");
                self.workflow.add(task);
                self.workflow.update_status(result, Status.COMPLETE);        
        
        
#        # Get the filled ones:
#        filled = [result for result in results if result.stepname == "FillPrescription"];
#        
#        if len(filled) > 0:
#            copyFields =['FirstName','LastName','Birthday'];
#            appendFields = ['prescriptionnumber', 'DrugName','GenericName','Refills'];
#            addFields = ['TotalCharge'];
#            task = Task.construct_from_results(filled, "Pharmacist", "DispensePrescription", copy=copyFields, append=appendFields, add_fields=addFields);
#            self.workflow.add(task);
#        for result in filled:
#            self.workflow.update_status(result, Status.COMPLETE);          
            
            
            
    def ship_complete(self, results):
        for result in results:
            #task = Task.construct_from_result(result, "roaster", "package");
            #self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);        
        
        
        
#        # Get the filled ones:
#        filled = [result for result in results if result.stepname == "FillPrescription"];
#        
#        if len(filled) > 0:
#            copyFields =['FirstName','LastName','Birthday'];
#            appendFields = ['prescriptionnumber', 'DrugName','GenericName','Refills'];
#            addFields = ['TotalCharge'];
#            task = Task.construct_from_results(filled, "Pharmacist", "DispensePrescription", copy=copyFields, append=appendFields, add_fields=addFields);
#            self.workflow.add(task);
#        for result in filled:
#            self.workflow.update_status(result, Status.COMPLETE);    
            
            
            
            

if __name__ == '__main__':
    backend = CoffeeBackend();